//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.lyyzoo.data.util;

import com.lyyzoo.util.Strings;
import java.util.ArrayList;
import java.util.List;

public class Sqls {
    private static final String[] COUNT_KEY_WORD = new String[]{" group ", " order ", " having ", " limit ", " procedure ", " for ", " lock "};
    private static final String TABLE_ALIAS = "table_alias";

    public Sqls() {
    }

    public static String substringCountBeforeKeyWord(String str) {
        return Strings.substringBeforeIgnoreCases(str, COUNT_KEY_WORD);
    }

    public static String buildCountSql(String sql) {
        String upperCaseSql = sql.toLowerCase();
        StringBuilder countSql = new StringBuilder();
        String prefix = "select count(1) ";
        String selectKey = "select";
        String fromKey = "from";
        if (Strings.isContainsIgnoreCase(sql, "group by")) {
            return prefix + fromKey + "(" + sql + ")" + "table_alias";
        } else {
            Integer fromRelIndex = upperCaseSql.indexOf(fromKey);
            List<Integer> fromIndexList = getStrKeyIndexList(upperCaseSql, fromKey);
            if (fromIndexList.size() > 1) {
                for(int i = 0; i < fromIndexList.size(); ++i) {
                    Integer selectKeyCount = getStrKeyIndexList(upperCaseSql.substring(0, (Integer)fromIndexList.get(i)), selectKey).size() - 1;
                    if (selectKeyCount == i) {
                        fromRelIndex = (Integer)fromIndexList.get(i);
                        break;
                    }
                }

                fromRelIndex = fromRelIndex - fromKey.length();
            }

            String afterFromSql = sql.substring(fromRelIndex);
            if (Strings.isContains(upperCaseSql, ")")) {
                upperCaseSql = Strings.substringAfterLast(upperCaseSql, ")");
            }

            if (Strings.isContainsIgnoreCase(upperCaseSql, "order")) {
                String query = Strings.substringAfterAndContainsIgnoreCases(upperCaseSql, " order by ");
                afterFromSql = afterFromSql.substring(0, afterFromSql.length() - query.length());
            }

            sql = countSql.append(prefix + afterFromSql).toString();
            return sql;
        }
    }

    public static String buildPaginateSql(String sql, int pageNumber, int pageSize) {
        return buildPaginateSql(sql, pageNumber, pageSize, true);
    }

    public static String buildPaginateSql(String sql, int pageNumber, int pageSize, boolean hasCount) {
        StringBuilder paginationSQL = new StringBuilder();
        if (pageNumber < 1) {
            pageNumber = 1;
        }

        if (pageSize == 0) {
            pageSize = 15;
        }

        int offset = hasCount ? pageSize * (pageNumber - 1) : (pageSize - 1) * (pageNumber - 1);
        paginationSQL.append(sql);
        paginationSQL.append(" limit ").append(offset).append(", ").append(pageSize);
        return paginationSQL.toString();
    }

    private static List<Integer> getStrKeyIndexList(String str, String key) {
        int temp = 0;
        ArrayList indexList = new ArrayList();

        while((temp = str.indexOf(key, temp)) != -1) {
            temp += key.length();
            indexList.add(temp);
        }

        return indexList;
    }
}
